package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.service.IBankAccountService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;

@Service
public class BankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount> implements IBankAccountService {
    /**
     * 新增或更新银行账户信息
     *
     * @param dto
     */
    @Override
    public void addOrUpdateBankAccount(BankAccountUpsertReqDTO dto) {
        // 从当前线程中获取用户信息
        CurrentUserInfo currentUserInfo = UserContext.currentUser();
        dto.setId(currentUserInfo.getId());
        dto.setType(currentUserInfo.getUserType());
        BankAccount bankAccount = BeanUtil.toBean(dto, BankAccount.class);
        super.saveOrUpdate(bankAccount);
    }

    /**
     * 获取当前用户银行账号
     *
     * @return
     */
    @Override
    public BankAccount selectCurrentUserBankAccount() {
        Long userId = UserContext.currentUserId();
        return lambdaQuery().eq(BankAccount::getId, userId).one();
    }
}
